/**
 *
 */
package com.zhuoyue.elmt.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zhuoyue.elmt.data.ResultData;
import com.zhuoyue.elmt.entity.PlaCommonQuestion;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eit.eitsite.common.config.Global;
import com.eit.eitsite.common.persistence.Page;
import com.eit.eitsite.common.web.BaseController;
import com.eit.eitsite.common.utils.StringUtils;
import com.zhuoyue.elmt.entity.PlaFeedback;
import com.zhuoyue.elmt.service.PlaFeedbackService;

import java.util.ArrayList;
import java.util.List;

/**
 * 意见反馈Controller
 *
 * @author JiangYH
 * @version 2017-01-13
 */
@Controller
@RequestMapping(value = "${adminPath}/elmt/plaFeedback")
public class PlaFeedbackController extends BaseController {

    @Autowired
    private PlaFeedbackService plaFeedbackService;

    @ModelAttribute
    public PlaFeedback get(@RequestParam(required = false) String id) {
        PlaFeedback entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = plaFeedbackService.get(id);
        }
        if (entity == null) {
            entity = new PlaFeedback();
        }
        return entity;
    }

    @RequiresPermissions("elmt:plaFeedback:view")
    @RequestMapping(value = {"list", ""})
    public String list(PlaFeedback plaFeedback, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<PlaFeedback> page = plaFeedbackService.findPage(new Page<PlaFeedback>(request, response), plaFeedback);
        model.addAttribute("page", page);
        return "zhuoyue/elmt/plaFeedbackList";
    }

    @RequiresPermissions("elmt:plaFeedback:view")
    @RequestMapping(value = "form")
    public String form(PlaFeedback plaFeedback, Model model) {
        model.addAttribute("plaFeedback", plaFeedback);
        return "zhuoyue/elmt/plaFeedbackForm";
    }

    @RequiresPermissions("elmt:plaFeedback:edit")
    @RequestMapping(value = "save")
    public String save(PlaFeedback plaFeedback, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, plaFeedback)) {
            return form(plaFeedback, model);
        }
        plaFeedbackService.save(plaFeedback);
        addMessage(redirectAttributes, "保存意见反馈成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/plaFeedback/?repage";
    }

    @RequiresPermissions("elmt:plaFeedback:edit")
    @RequestMapping(value = "delete")
    public String delete(PlaFeedback plaFeedback, RedirectAttributes redirectAttributes) {
        plaFeedbackService.delete(plaFeedback);
        addMessage(redirectAttributes, "删除意见反馈成功");
        return "redirect:" + Global.getAdminPath() + "/elmt/plaFeedback/?repage";
    }

    @RequestMapping(value = "mobilesave", method = RequestMethod.POST)
    public String mobilesave(HttpServletResponse response, PlaFeedback plaFeedback, Model model, RedirectAttributes redirectAttributes) {
        ResultData<List<PlaFeedback>> resultData = new ResultData();
        try {
            logger.info("增加意见反馈");
            plaFeedbackService.save(plaFeedback);
            List<PlaFeedback> list = new ArrayList<PlaFeedback>();
            list.add(plaFeedback);
            resultData.setList(list);
            resultData.setCode("SUCC");
            resultData.setMessage("成功");
        } catch (Exception e) {
            resultData.setCode("FAIL");
            resultData.setMessage("增加意见反馈出现异常！");
            e.printStackTrace();
        }
        return super.renderString(response, resultData);
    }

}